package domain;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import com.google.code.linkedinapi.schema.Certification;
import com.google.code.linkedinapi.schema.Certifications;
import com.google.code.linkedinapi.schema.DateOfBirth;
import com.google.code.linkedinapi.schema.Education;
import com.google.code.linkedinapi.schema.Educations;
import com.google.code.linkedinapi.schema.Language;
import com.google.code.linkedinapi.schema.Languages;
import com.google.code.linkedinapi.schema.Location;
import com.google.code.linkedinapi.schema.Person;
import com.google.code.linkedinapi.schema.PhoneNumber;
import com.google.code.linkedinapi.schema.PhoneNumbers;
import com.google.code.linkedinapi.schema.Position;
import com.google.code.linkedinapi.schema.Recommendation;
import com.google.code.linkedinapi.schema.RecommendationsReceived;
import com.google.code.linkedinapi.schema.Skill;
import com.google.code.linkedinapi.schema.Skills;

@Entity
@Table(name = "linkedinprofile")
public class LinkedInProfile extends PersistentEntity {

	public static final String LINKEDIN_URL = "http://www.linkedin.com/profile/view?id="; 
	
	private String associations;
	private String certifications;
	private String currentStatus;
	private String lastStatusUpdate; // Este valor se saca del long currentStatusTimestamp
	private String dateOfBirth;
	private Long distance;
	@Column(length=4000)
	private String educations;
	private String firstName;
	private String headline;
	private String honors;
	private String linkedinId;
	@Column(length=4000)
	private String industry;
	@Column(length=4000)
	private String interests;
	private String languages;
	private String lastName;
	private String locations;
	private String mainAddress;
	private Long numConnections;
	private Long numRecommenders;
	private String path;
	private String phoneNumbers;
	private String pictureUrl;
	@Column(length=4000)
	private String positions;
	@Column(length=4000)
	private String publications;
	private String publicProfileUrl;
	@Column(length=4000)
	private String recommendationsReceived;
	@Column(length=4000)
	private String skills;
	@Column(length=4000)
	private String specialties;
	@Column(length=4000)
	private String summary;
	@Column(length=4000)
	private String threeCurrentPositions;
	@Column(length=4000)
	private String threePastPositions;

	private boolean batchUpdated;
	
	public LinkedInProfile(){
		super();
		this.batchUpdated = false;
	}
	
	public void setImportedProfile(Person linkedInPerson) {
		this.linkedinId = linkedInPerson.getId();
		this.associations = linkedInPerson.getAssociations();
		this.certifications = getConvertedCertifications(linkedInPerson.getCertifications());
		this.currentStatus = linkedInPerson.getCurrentStatus();
		this.lastStatusUpdate = getConvertedCurrentStatusTimestamp(linkedInPerson.getCurrentStatusTimestamp());
		this.dateOfBirth = getConvertedDate(linkedInPerson.getDateOfBirth());
		this.distance = linkedInPerson.getDistance();
		this.educations = getConvertedEducations(linkedInPerson.getEducations());
		this.firstName = linkedInPerson.getFirstName();
		this.headline = linkedInPerson.getHeadline();
		this.honors = linkedInPerson.getHonors();
		this.industry = linkedInPerson.getIndustry();
		this.interests = linkedInPerson.getInterests();
		this.languages = getConvertedLanguages(linkedInPerson.getLanguages());
		this.lastName = linkedInPerson.getLastName();
		this.locations = getConvertedLocations(linkedInPerson.getLocation());
		this.mainAddress = linkedInPerson.getMainAddress();
		this.numConnections = linkedInPerson.getNumConnections();
		this.numRecommenders = linkedInPerson.getNumRecommenders();
		this.path = linkedInPerson.getPath();
		this.phoneNumbers = getConvertedPhoneNumbers(linkedInPerson.getPhoneNumbers());
		this.pictureUrl = linkedInPerson.getPictureUrl();
		this.positions = linkedInPerson.getPositions() == null ? null : getConvertedPositions(linkedInPerson.getPositions().getPositionList());
		this.publicProfileUrl = linkedInPerson.getPublicProfileUrl();
		this.recommendationsReceived = getConvertedRecommendations(linkedInPerson.getRecommendationsReceived());
		this.skills = getConvertedSkills(linkedInPerson.getSkills());
		this.specialties = linkedInPerson.getSpecialties();
		this.summary = linkedInPerson.getSummary();
		this.threeCurrentPositions = linkedInPerson.getThreeCurrentPositions() == null ? null : getConvertedPositions(linkedInPerson.getThreeCurrentPositions().getPositionList());
		this.threePastPositions = linkedInPerson.getThreePastPositions() == null ? null : getConvertedPositions(linkedInPerson.getThreePastPositions().getPositionList());
	}

	public String getPrivateProfileUrl() {
		if (this.linkedinId == null) {
			return null;
		}
		//TODO: esto me manda a cualquier lado
		return LINKEDIN_URL + this.linkedinId;
	}
	
	private String getConvertedCurrentStatusTimestamp(Long timestamp){
		if (timestamp != null) {
			Date date = new Date((long)timestamp);
			SimpleDateFormat formatObject = new SimpleDateFormat("dd/MM/yyyy");
			return formatObject.format(date);
		} else {
			return "";
		}
	}
	
	private String getConvertedDate(DateOfBirth dateOfBirth) {
		String date = "";
		if (dateOfBirth != null) {
			date = dateOfBirth.getDay() + "/" + dateOfBirth.getMonth();
			if (dateOfBirth.getYear() != null){
				date = date + "/" + dateOfBirth.getYear();
			}
		}
		return date;
	}

	private String getConvertedSkills(Skills skills2) {
		String skillsString = null;
		if (skills2 != null) {
			List<Skill> skills = skills2.getSkillList();
			if (!skills.isEmpty()){
				skillsString = "<ul>";
				//int i = 1;
				for (Skill s: skills) {
					//TODO aca hay un millon de cosas, habria que ver como ponerlas
					skillsString += "<li>";
					if (s.getSkill()!=null){
						skillsString += s.getSkill().getName();
					}
					if (s.getProficiency()!=null){
						skillsString += ", " + s.getProficiency().getName() + ", " + s.getProficiency().getLevel();
					}
					if (s.getYears()!=null){
						skillsString += ", " + s.getYears() + " años";	
					}
					//if (i++ < skills.size()) {
					//	skillsString += " / ";
					//}
					skillsString += "</li>";
				}
				skillsString += "</ul>";
			}
		}
		return skillsString;
	}

	private String getConvertedRecommendations(RecommendationsReceived recommendationsReceived2) {
		String recommendationsReceivedString = null;
		if (recommendationsReceived2 != null) {
			List<Recommendation> recommendationsReceived = recommendationsReceived2.getRecommendationList();
			if (!recommendationsReceived.isEmpty()){
				recommendationsReceivedString = "<ul>";
				//int i = 1;
				for (Recommendation p: recommendationsReceived) {
					if (p.getRecommendationText() != null) {
						recommendationsReceivedString += "<li>";
						recommendationsReceivedString += p.getRecommendationText();
						recommendationsReceivedString += "</li>";
						//if (i++ < recommendationsReceived.size()) {
						//	recommendationsReceivedString += " / ";
						//}
					}
				}
				recommendationsReceivedString += "</ul>";
			}
		}
		return recommendationsReceivedString;
	}

	private String getConvertedPositions(List<Position> positions) {
		String positionsString = null;
		if (positions != null) {
			if (!positions.isEmpty()){
				positionsString = "<ul>";
				//int i = 1;
				for (Position p: positions) {
					//TODO aca hay un millon de cosas, habria que ver como ponerlas
					positionsString += "<li>";
					positionsString += p.getTitle() + ", " + p.getCompany().getName() + ", " + p.getCompany().getIndustry() + ", " + p.getSummary();
					positionsString += "</li>";
					//if (i++ < positions.size()) {
					//	positionsString += " / ";
					//}
				}
				positionsString += "</ul>";
			}
		}
		return positionsString;
	}

	private String getConvertedPhoneNumbers(PhoneNumbers linkedinPN) {
		String phoneNumbersString = null;
		if (linkedinPN != null) {
			List<PhoneNumber> phoneNumbers = linkedinPN.getPhoneNumberList();
			if (!phoneNumbers.isEmpty()){
				phoneNumbersString = "<ul>";
				//int i = 1;
				for (PhoneNumber p: phoneNumbers) {
					phoneNumbersString += "<li>";
					phoneNumbersString += p.getPhoneNumber() + ", " + p.getPhoneType();
					phoneNumbersString += "</li>";
					//if (i++ < phoneNumbers.size()) {
					//	phoneNumbersString += " / ";
					//}
				}
				phoneNumbersString += "</ul>";
			}
		}
		return phoneNumbersString;
	}

	private String getConvertedLocations(Location linkedinLocation) {
		return (linkedinLocation != null && linkedinLocation.getName() != null) ? linkedinLocation.getName() : null;
	}

	private String getConvertedLanguages(Languages linkedinLanguages) {
		String languagesString = null;
		if (linkedinLanguages != null) {
			List<Language> languages = linkedinLanguages.getLanguageList();
			if (!languages.isEmpty()){
				languagesString = "<ul>";
				//int i = 1;
				for (Language l: languages) {
					languagesString += "<li>";
					if (l.getLanguage()!=null){
						languagesString += l.getLanguage().getName();	
					}
					if (l.getProficiency()!=null){
						languagesString += ", " + l.getProficiency().getName() + ", " + l.getProficiency().getLevel().name(); 
					}
					languagesString += "</li>";
					//if (i++ < languages.size()) {
					//	languagesString += " / ";
					//}
				}
				languagesString += "</ul>";
			}
		}
		return languagesString;
	}

	private String getConvertedEducations(Educations linkedinEducations) {
		String educationsString = null;
		if (linkedinEducations != null) {
			List<Education> educations = linkedinEducations.getEducationList();
			if (!educations.isEmpty()){
				educationsString = "<ul>";
				//int i = 1;
				for (Education e: educations) {
					educationsString += "<li>";
					educationsString += e.getSchoolName() + ", " + e.getDegree() + ", From: " + e.getStartDate().getYear() + ", To: " + e.getEndDate().getYear();
					educationsString += "</li>";
					//if (i++ < educations.size()) {
					//	educationsString += " / ";
					//}
				}
				educationsString += "</ul>";
			}
		}
		return educationsString;
	}

	private String getConvertedCertifications(Certifications linkedinCert) {
		String certificationString = null;
		if (linkedinCert != null) {
			List<Certification> certifications = linkedinCert.getCertificationList();
			if (!certifications.isEmpty()){
				//int i = 1;
				certificationString = "<ul>";
				for (Certification c: certifications) {
					certificationString += "<li>";
					certificationString += c.getName() + ", " + c.getAuthority() + ", From: " + c.getStartDate().getYear() + ", To: " + c.getEndDate().getYear();
					certificationString += "</li>";
					//if (i++ < certifications.size()) {
					//	certificationString += " / ";
					//}
				}
				certificationString += "</ul>";
			}
		}
		return certificationString;
	}

	@Override
	public String toString() {
		return "Profile [id=" + this.linkedinId + "]";
	}

	public String getAssociations() {
		return associations;
	}

	public String getCertifications() {
		return certifications;
	}

	public String getCurrentStatus() {
		return currentStatus;
	}

	public String getLastStatusUpdate() {
		return lastStatusUpdate;
	}

	public String getDateOfBirth() {
		return dateOfBirth;
	}

	public Long getDistance() {
		return distance;
	}

	public String getEducations() {
		return educations;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getHeadline() {
		return headline;
	}

	public String getHonors() {
		return honors;
	}

	public String getLinkedinId() {
		return linkedinId;
	}

	public String getIndustry() {
		return industry;
	}

	public String getInterests() {
		return interests;
	}

	public String getLanguages() {
		return languages;
	}

	public String getLastName() {
		return lastName;
	}

	public String getLocations() {
		return locations;
	}

	public String getMainAddress() {
		return mainAddress;
	}

	public Long getNumConnections() {
		return numConnections;
	}

	public Long getNumRecommenders() {
		return numRecommenders;
	}

	public String getPath() {
		return path;
	}

	public String getPhoneNumbers() {
		return phoneNumbers;
	}

	public String getPictureUrl() {
		return pictureUrl;
	}

	public String getPositions() {
		return positions;
	}

	public String getPublications() {
		return publications;
	}

	public String getPublicProfileUrl() {
		return publicProfileUrl;
	}

	public String getRecommendationsReceived() {
		return recommendationsReceived;
	}

	public String getSkills() {
		return skills;
	}

	public String getSpecialties() {
		return specialties;
	}

	public String getSummary() {
		return summary;
	}

	public String getThreeCurrentPositions() {
		return threeCurrentPositions;
	}

	public String getThreePastPositions() {
		return threePastPositions;
	}

	public boolean isBatchUpdated() {
		return batchUpdated;
	}

	public void setBatchUpdated(boolean batchUpdated) {
		this.batchUpdated = batchUpdated;
	}
	
}